package com.cn.sys.main.web;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cn.sys.frame.util.page.PageData;
import com.cn.sys.frame.util.query.QueryBean;
import com.cn.sys.frame.util.query.QueryCondition;
import com.cn.sys.frame.web.BaseController;
import com.cn.sys.main.entity.SysOrgan;
import com.cn.sys.main.entity.SysUser;
import com.cn.sys.main.service.SysOrganService;

@Controller
@RequestMapping("sys/organ")
public class SysOrganController extends BaseController {

	@Autowired
	private SysOrganService sysOrganService;
	
	@RequestMapping(value = "/toList.do", method = RequestMethod.GET)
	public String toList(HttpServletRequest request, Model model){
		return "sys/organ/organList";
	}
	
	@RequestMapping(value = "/getOrganTree.do", method = RequestMethod.POST)
	@ResponseBody
	public Object getOrganTree() throws Exception{
		
		return sysOrganService.getOrganTreeList();
	}
	
	@RequestMapping(value = "/list.do", method = RequestMethod.POST)
	@ResponseBody
	public Object list(HttpServletRequest request, String organName, String parentId) throws Exception{
		PageData<SysOrgan> pageData = createPageDataFromRequest(request);
		QueryCondition queryCondition = null;
		if(StringUtils.isNotBlank(organName)){
			QueryBean qb = new QueryBean(" AND NAME LIKE ?", "NAME", "%"+organName+"%");
			queryCondition = new QueryCondition();
			queryCondition.setQueryBean(qb);
		}
		if(StringUtils.isNotBlank(parentId)){
			QueryBean qb = new QueryBean(" AND PARENT_ID = ?", "NAME", Long.valueOf(parentId));
			queryCondition = new QueryCondition();
			queryCondition.setQueryBean(qb);
		}
		sysOrganService.getPageData(pageData, queryCondition);
		return pageData.getPageDataMap();
	}
	
	@RequestMapping(value = "/toAdd.do", method = RequestMethod.GET)
	public String toAdd(String id, Model model) throws Exception{
		if(StringUtils.isNotBlank(id)&&StringUtils.isNumeric(id)){
			SysOrgan organ = sysOrganService.get(Long.valueOf(id));
			model.addAttribute("organ", organ);
		}
		return "sys/organ/organAdd";
	}
	
	@RequestMapping(value = "/toView.do", method = RequestMethod.GET)
	public String toView(String id, Model model) throws Exception{
		if(StringUtils.isNotBlank(id)&&StringUtils.isNumeric(id)){
			SysOrgan organ = sysOrganService.get(Long.valueOf(id));
			model.addAttribute("organ", organ);
			if(organ.getParentId()!=null){
				SysOrgan parentOrgan = sysOrganService.get(organ.getParentId());
				model.addAttribute("parentName", parentOrgan.getName());
			}
		}
		return "sys/organ/organView";
	}
	
	@RequestMapping(value = "/validateOrganName.do", method = RequestMethod.POST)
	@ResponseBody
	public Object validateRoleName(String id, String name) throws Exception{
		QueryCondition queryCondition = new QueryCondition();
		if(StringUtils.isNotBlank(id)){
			QueryBean qb = new QueryBean(" AND ID <> ?", "ID", Long.valueOf(id));
			queryCondition.setQueryBean(qb);
		}
		if(StringUtils.isNotBlank(name)){
			queryCondition.setQueryBean(new QueryBean(" AND NAME = ?", "NAME", name));
		}else {
			return false;
		}
		long count = sysOrganService.getCount(queryCondition);
		if(count>0){
			return false;
		}
		return true;
	}
	
	@RequestMapping(value = "/save.do", method = RequestMethod.POST)
	@ResponseBody
	public Object save(HttpServletRequest request, SysOrgan organ) throws Exception{
		if(organ!=null && organ.getId()!=null){
			SysOrgan oldOrgan = sysOrganService.get(organ.getId());
			organ.setCreateTime(oldOrgan.getCreateTime());
			organ.setCreateUser(oldOrgan.getCreateUser());
			organ.setUpdateTime(new Date());
			organ = sysOrganService.update(organ);
		}else {
			organ.setCreateTime(new Date());
			SysUser currentUser = getSessionUser(request);
			organ.setCreateUser(currentUser!=null?currentUser.getUserName():null);
			organ = sysOrganService.save(organ);
		}
		Map<String, Object> result = new HashMap<String, Object>(1);
		if(organ==null){
			result.put("success", false);
			
		}else {
			result.put("success", true);
		}
		return result;
	}
	
	@RequestMapping(value = "/delete.do", method = RequestMethod.POST)
	@ResponseBody
	public Object delete(HttpServletRequest request, String id) throws Exception{
		Map<String, Object> result = new HashMap<String, Object>(2);
		if(id!=null){
			QueryCondition queryCondition = new QueryCondition();
			QueryBean qb = new QueryBean("AND PARENT_ID = ?", "PARENT_ID", Long.valueOf(id));
			queryCondition.setQueryBean(qb);
			long cnt = sysOrganService.getCount(queryCondition);
			if(cnt<=0){
				int resultCnt = sysOrganService.delete(Long.valueOf(id));
				if(resultCnt==1){
					result.put("success", true);
				}else {
					result.put("success", false);
				}
			}else{
				result.put("success", false);
				result.put("msg", "该组织机构下子机构，请先删除子机构!");
			}
		}else {
			result.put("success", false);
		}
		return result;
	}
}
